package com.budwk.app.wxprogram.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.wxprogram.models.Disaster_opinion;
import com.budwk.app.wxprogram.services.DisasterOpinionService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@IocBean
@At("/platform/wxprogram/opinion")
@ApiDefinition(tag = "灾害舆情热点榜表")
@Slf4j
public class DisasterOpinionController{
    @Inject
    private DisasterOpinionService disasterOpinionService;

    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询灾害舆情热点榜表")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = Disaster_opinion.class
    )
    @ApiResponses(
    implementation = Disaster_opinion.class
    )
    @SaCheckPermission("wxprogram.opinion")
    public Result<?> list(@Param("..")Disaster_opinion bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getSort() !=null && Strings.isNotBlank(bean.getSort().toString())) {
            cnd.and("sort","like","%"+bean.getSort()+"%");
        }
        
        if (bean.getDisaster_number() !=null && Strings.isNotBlank(bean.getDisaster_number().toString())) {
            cnd.and("disaster_number","like","%"+bean.getDisaster_number()+"%");
        }
        
        if (bean.getDisaster_type() !=null && Strings.isNotBlank(bean.getDisaster_type().toString())) {
            cnd.and("disaster_type","like","%"+bean.getDisaster_type()+"%");
        }
        
        if (bean.getProvince() !=null && Strings.isNotBlank(bean.getProvince().toString())) {
            cnd.and("province","like","%"+bean.getProvince()+"%");
        }
        
        if (bean.getCity() !=null && Strings.isNotBlank(bean.getCity().toString())) {
            cnd.and("city","like","%"+bean.getCity()+"%");
        }
        
        if (bean.getDistrict() !=null && Strings.isNotBlank(bean.getDistrict().toString())) {
            cnd.and("district","like","%"+bean.getDistrict()+"%");
        }
        
        if (bean.getDisaster_date() !=null && Strings.isNotBlank(bean.getDisaster_date().toString())) {
            Instant instant = bean.getDisaster_date().toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String startdateStr = localDateTime.format(fmt);
            String enddateStr = localDateTime.plusDays(1).format(fmt);
            cnd.and("disaster_date","between",new Object[]{startdateStr,enddateStr});
        }
        
        if (bean.getTitle_create_date() !=null && Strings.isNotBlank(bean.getTitle_create_date().toString())) {
            Instant instant = bean.getTitle_create_date().toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String startdateStr = localDateTime.format(fmt);
            String enddateStr = localDateTime.plusDays(1).format(fmt);
            cnd.and("title_create_date","between",new Object[]{startdateStr,enddateStr});
        }
        
        if (bean.getInformation_title() !=null && Strings.isNotBlank(bean.getInformation_title().toString())) {
            cnd.and("information_title","like","%"+bean.getInformation_title()+"%");
        }
        
        if (bean.getDisaster_title() !=null && Strings.isNotBlank(bean.getDisaster_title().toString())) {
            cnd.and("disaster_title","like","%"+bean.getDisaster_title()+"%");
        }
        
        return Result.data(disasterOpinionService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增灾害舆情热点榜表")
    @ApiFormParams(
        implementation = Disaster_opinion.class
    )
    @ApiResponses
    @SLog(tag = "新增灾害舆情热点榜表", value = "${args[0].id}")
    @SaCheckPermission("wxprogram.opinion.create")
    public Result<?> create(@Param("..")Disaster_opinion disasterOpinion, HttpServletRequest req) {
        disasterOpinion.setCreatedBy(SecurityUtil.getUserId());
        disasterOpinionService.insert(disasterOpinion);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改灾害舆情热点榜表")
    @ApiFormParams(
        implementation = Disaster_opinion.class
    )
    @ApiResponses
    @SLog(tag = "修改灾害舆情热点榜表", value = "${args[0].id}")
    @SaCheckPermission("wxprogram.opinion.update")
    public Result<?> update(@Param("..")Disaster_opinion disasterOpinion, HttpServletRequest req) {
        disasterOpinion.setUpdatedBy(SecurityUtil.getUserId());
        disasterOpinionService.updateIgnoreNull(disasterOpinion);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取灾害舆情热点榜表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("wxprogram.opinion")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        Disaster_opinion disasterOpinion = disasterOpinionService.fetch(id);
        if (disasterOpinion == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(disasterOpinion);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除灾害舆情热点榜表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除灾害舆情热点榜表", value = "${args[0]}")
    @SaCheckPermission("wxprogram.opinion.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        Disaster_opinion disasterOpinion = disasterOpinionService.fetch(id);
        if (disasterOpinion == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        disasterOpinionService.delete(id);
        req.setAttribute("_slog_msg", disasterOpinion.getId());
        return Result.success();
    }
}
